#include "core.h"
#include "../../ExEngine/common/def.h"
#include "../../ExEngine/engine/object.h"
#include "../../ExEngine/engine/engine.h"

#include "../../ExEngine/input/dinput.h"
#include "../../ExEngine/render/d3d.h"
#include "../../ExEngine/sound/dsound.h"

void Game::CameraMovement(Matrix4f* pTransform)
{
	Vector3f targetCameraPosition;
	Vector3f targetCameraLookAt;

	CCamera* pCamera = CEngine::GetInstance()->GetCamera();

	switch(Game::g_GamePlayMode) {
		case PlayMode::SIDE_SCROLL: case PlayMode::CHASE:
			{
				targetCameraPosition = g_Player->CameraAnchor();
				targetCameraLookAt	=  g_Player->CameraTarget();
			}break;

		case PlayMode::TRANSITION:
			{				
				targetCameraPosition = Game::g_TransitionCameraAnchor;
				targetCameraLookAt	=  Game::g_TransitionCameraTarget;
			}break;
		case PlayMode::FREE_FLY:
			{
				CDInput9Device* pInput = CEngine::GetInstance()->GetInput();
				Vector3f movement;
				Vector2i mouse = pInput->getMouseDelta();
				if(pInput->isKeyDown(DIK_W))
					movement.z++;
				if(pInput->isKeyDown(DIK_S))
					movement.z--;
				if(pInput->isKeyDown(DIK_A))
					movement.x++;
				if(pInput->isKeyDown(DIK_D))
					movement.x--;

				CCamera::ApplyFreeview(pCamera, movement, mouse.x, mouse.y);
				return;

			}break;
		default:{

			return;
				}break;
	}

	const Vector3f cameraPosition = pCamera->getPosition();
	const Vector3f cameraMove = (targetCameraPosition - cameraPosition) * 0.25f;
	const Vector3f cameraEndPos = cameraPosition + cameraMove;

	Vector3f cameraDir = targetCameraLookAt  - cameraEndPos;
	Vector3f::normalize(&cameraDir);
	
	pCamera->setPosition(cameraEndPos);
	pCamera->setDirection(cameraDir);
}
